/*
 * 作者: junmu
 * 时间: 2025-07-22
 */
package com.hzbc.faceattendance.controller;

import com.hzbc.faceattendance.dto.ApiResponse;
import com.hzbc.faceattendance.dto.AttendanceQueryRequest;
import com.hzbc.faceattendance.dto.PushMessageQueryRequest;
import com.hzbc.faceattendance.dto.PushMessageRequest;
import com.hzbc.faceattendance.model.PushMessage;
import com.hzbc.faceattendance.service.IWechatPushService;
import com.hzbc.faceattendance.utils.DateRangeValidator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Tag(name = "消息推送管理", description = "推送消息相关接口")
@RestController
@RequestMapping("/api/push")
public class PushMessageController {

    @Autowired
    private IWechatPushService wechatPushService;

    // ... 其他代码不变 ...

    @Operation(summary = "获取所有消息", description = "根据日期范围获取所有推送消息")
    @PostMapping("/all")
    public ApiResponse<Page<PushMessage>> getAllMessages(  // 修改返回类型为Page
                                                           @RequestBody AttendanceQueryRequest request,
                                                           @PageableDefault(size = 20) Pageable pageable) {

        ApiResponse<?> errorResponse = DateRangeValidator.validateForResponse(request);
        if (errorResponse != null) {
            return (ApiResponse<Page<PushMessage>>) errorResponse;
        }
        Date[] dates = DateRangeValidator.validateAndParse(request);
        Page<PushMessage> messages = wechatPushService.findByCreateTimeBetween(dates[0], dates[1], pageable); // 修改服务方法
        return new ApiResponse<>(true, 0, "获取所有消息成功", messages);
    }

    @Operation(summary = "获取异常消息", description = "根据日期范围获取异常类型推送消息")
    @PostMapping("/abnormal")
    public ApiResponse<Page<PushMessage>> getAbnormalMessages(  // 修改返回类型为Page
                                                                @RequestBody AttendanceQueryRequest request,
                                                                @PageableDefault(size = 20) Pageable pageable) {

        ApiResponse<?> errorResponse = DateRangeValidator.validateForResponse(request);
        if (errorResponse != null) {
            return (ApiResponse<Page<PushMessage>>) errorResponse;
        }

        Date[] dates = DateRangeValidator.validateAndParse(request);
        Page<PushMessage> messages = wechatPushService.findByTypeAndCreateTimeBetween(  // 修改服务方法
                1, dates[0], dates[1], pageable);
        return new ApiResponse<>(true, 0, "获取异常消息成功", messages);
    }

    @Operation(summary = "获取告警消息", description = "根据日期范围获取告警类型推送消息，默认获取当天消息")
    @PostMapping("/alert")
    public ApiResponse<Page<PushMessage>> getAlertMessages(  // 修改返回类型为Page
                                                             @RequestBody AttendanceQueryRequest request,
                                                             @PageableDefault(size = 20) Pageable pageable) {
        ApiResponse<?> errorResponse = DateRangeValidator.validateForResponse(request);
        if (errorResponse != null) {
            return (ApiResponse<Page<PushMessage>>) errorResponse;
        }

        Date[] dates = DateRangeValidator.validateAndParse(request);
        Page<PushMessage> messages = wechatPushService.findByTypeAndCreateTimeBetween(  // 修改服务方法
                2, dates[0], dates[1], pageable);
        return new ApiResponse<>(true, 0, "获取告警消息成功", messages);
    }

    @Operation(summary = "获取未处理消息", description = "根据日期范围获取未处理推送消息")
    @PostMapping("/unprocessed")
    public ApiResponse<Page<PushMessage>> getUnprocessedMessages(
            @RequestBody AttendanceQueryRequest request,
            @PageableDefault(size = 20) Pageable pageable) {

        ApiResponse<?> errorResponse = DateRangeValidator.validateForResponse(request);
        if (errorResponse != null) {
            return (ApiResponse<Page<PushMessage>>) errorResponse;
        }

        Date[] dates = DateRangeValidator.validateAndParse(request);
        Page<PushMessage> messages = wechatPushService.findUnprocessedMessages(dates[0], dates[1], pageable);
        return new ApiResponse<>(true, 0, "获取未处理消息成功", messages);
    }

    @Operation(summary = "根据类型和状态查询消息", description = "根据消息类型和状态查询推送消息")
    @PostMapping("/queryByTypeAndStatus")
    public ApiResponse<Page<PushMessage>> findPushMessagesByTypeAndStatus(@RequestBody PushMessageQueryRequest request,@PageableDefault(size = 20, sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        if (request.getStartDate() == null || request.getEndDate() == null ||
                request.getStartDate().trim().isEmpty() || request.getEndDate().trim().isEmpty()) {
            throw new IllegalArgumentException("开始日期和结束日期不能为空");
        }
        Page<PushMessage> messages = wechatPushService.findPushMessagesByTypeAndStatus(request, pageable);
        return new ApiResponse<>(true, 0, "获取消息成功", messages);
    }
    @Operation(summary = "根据ID查询消息", description = "根据消息ID查询推送消息")
    @GetMapping("/getById/{messageId}")
    public ApiResponse<PushMessage> getPushMessageById(@PathVariable("messageId") Long messageId) {
        try {
            PushMessage message = wechatPushService.findById(messageId);
            return new ApiResponse<>(true, 0, "获取消息成功", message);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse<>(false, 1, "获取消息失败", null);
        }
    }

    @Operation(summary = "处理消息", description = "标记消息为已处理状态并记录处理人")
    @PostMapping("/process")
    public ApiResponse<PushMessage> processMessage(@RequestBody PushMessageRequest request) {
        try {
            PushMessage message = wechatPushService.findById(request.getMessageId());
            message.setPushStatus(1);
            message.setHandlerId(request.getUserId());
            message.setHandleTime(new Date());
            PushMessage processedMessage = wechatPushService.save(message);
            return new ApiResponse<>(true, 0, "消息处理成功", processedMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse<>(false, 1, "消息处理失败", null);
        }
    }

}